'use strict';//全部正在上映
const Controller = require('egg').Controller;
class all_showingController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 16 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,title,img from all_showing where title like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from all_showing where title like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id,title,img from all_showing where title ='%${keyname}%' or title  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from title where title like '%${keyname}%' or title like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id,title,img from all_showing limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from all_showing`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }
    async create() {
        const { ctx, app } = this;
        const { title,img } = ctx.request.body;
        const result = await app.mysql.insert('all_showing', { title,img })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "添加成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 406,
                msg: "添加失败"
            };
        }
    }
    async update() {
        const { ctx, app } = this;
        const { title,img } = ctx.request.body;
        const { id } = ctx.params;
        const row = {
            
            title,
            img
           
        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('all_showing', row, options)
        if (title,img) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`all_showing`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = all_showingController;
